﻿function PandaDKP:TableDelete(tab)
   local k,v
   for k,v in pairs(tab) do
      if type(tab[k]) == "table" then
	 self:TableDelete(tab[k])
      end
      tab[k] = nil
   end
   tab = nil
end
function PandaDKP:DeleteTable(tab) self:TableDelete(tab) end

-- Returns a table indexed by the the names of the people in the raid.
-- Each table value is an integer set to 0
function PandaDKP:BuildRaidmemberList()
   local tab = {}
   local i = 1
   while i ~= 41 do
      local name = GetRaidRosterInfo(i)
      if name ~= nil then
	 tab[name] = 0
      end
      i = i + 1
   end
   return tab
end

-- Given a name/word, captilize the first letter and lowercase the rest.
-- Return the result
function PandaDKP:ProperNameCaps(name)
   return string.upper(string.sub(name, 1, 1))..string.lower(string.sub(name, 2))
end

function PandaDKP:rgbToHex(rgb)
   return string.format("%02x%02x%02x", rgb.r*255, rgb.g*255, rgb.b*255)
end

function PandaDKP:getClassHex(class)
   if (class == "Death Knight") then class = "DEATHKNIGHT" end
   if (nil ~= class) then
      local color = RAID_CLASS_COLORS[string.upper(class)]
      if (nil ~= color) then return PandaDKP:rgbToHex(color) end
   end
   
   return "ffffff"
end

-- Return the current server date/time as a string to be used as a timestamp
function PandaDKP:GetTimestamp()
   -- Get the date and time from the server
   local w, month, day, year = CalendarGetDate()
   local hour,minute = GetGameTime()
   local seconds = time() % 60
   
   return string.format("%.4d%.2d%.2d%.2d%.2d%.2d", year, month, day, hour, minute, seconds)
end

-- Returns the current server date (time = 00:00:00) as a string, or the date portion of the specified timestamp
function PandaDKP:GetDatestamp(timestamp)
   local year, month, day
   
   if ("string" == type(timestamp)) then
      _, _, year, month, day = strfind(timestamp, '(%d%d%d%d)(%d%d)(%d%d)%d%d%d%d%d%d')
   else
      _, month, day, year = CalendarGetDate()
   end
   
   return string.format("%.4d%.2d%.2d000000", year, month, day)
end

-- Adds the specified number of seconds onto the specified timestamp
function PandaDKP:TimestampAdd(timestamp, addSeconds)
   -- Get the components of the timestamp
   local _, _, year, month, day, hour, minute, seconds = strfind(timestamp, '(%d%d%d%d)(%d%d)(%d%d)(%d%d)(%d%d)(%d%d)')
   
   -- Translate into a time (seconds since epoch) and add the parameter (may be negative)
   local time = time({year=year, month=month, day=day, hour=hour, min=minute, sec=seconds})
   time = time + addSeconds
   
   -- Translate back to a string
   return date('%Y%m%d%H%M%S', time)
end

function PandaDKP:TimestampToTimeString(timestamp)
   -- Get the components of the timestamp
   local _, _, year, month, day, hour, minute, seconds = strfind(timestamp, '(%d%d%d%d)(%d%d)(%d%d)(%d%d)(%d%d)(%d%d)')
   
   -- Translate into a time (seconds since epoch)
   local time = time({year=year, month=month, day=day, hour=hour, min=minute, sec=seconds})
   
   -- Translate back to a string
   return date('%Y-%m-%d %H:%M:%S', time)
end


-- Given the serialized version of a CharInfo struct, build the table
-- and return it
-- String form: "<name>|<class>|<level>|<race>"
function PandaDKP:StringToCharInfo(str)
   local name, class, level, race = strsplit("|", str)
   return { 
      name = name, class = class, level = tonumber(level), race = race,
      classhex = self:getClassHex(class) --,  guild = guild,
   }
end

-- Given an itemLink, extract and return the itemID string
function PandaDKP:ExtractItemID(itemLink)
   local _, _, itemID = string.find(itemLink, "|Hitem:(%d+):")
   -- "|Hitem:(.+)|h%[.+%]"
   return itemID
end


function PandaDKP:ParseCommand(text)

	local command = text;
	local parameters = text;
	if (command) then
		local index = strfind(command, " ");
		if (index) then
			command = strsub(command, 1, index-1);
			parameters = strsub(parameters, index+1);
		else
			parameters = "";
		end
	end

	return command, parameters;
	
end